package com.choosefine.base.shixiaobao.basedata.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.choosefine.base.shixiaobao.base.BaseService;
import com.choosefine.base.shixiaobao.base.dto.AttendanceDepartmentUpdateOrder;
import com.choosefine.base.shixiaobao.basedata.dao.AttendanceDepartmentMapper;
import com.choosefine.base.shixiaobao.basedata.model.AttendanceDepartment;
import com.choosefine.base.shixiaobao.common.def.CommonConstant;

/**
 * Comments：考勤-部门
 * Author：xiongchao
 * Create Date：Jul 26, 2017
 * Modified By：
 * Modified Date：
 * Why & What is modified：
 * Version：v1.0
 */
@Service("DepartmentInfoService")
public class AttendanceDepartmentService extends BaseService {

	@Autowired
	AttendanceDepartmentMapper attendanceDepartmentMapper;

	public AttendanceDepartment getDepartmentById(Integer id) throws Exception {
		return attendanceDepartmentMapper.selectByPrimaryKey(id);
	}

	public Integer save(AttendanceDepartment attendanceDepartment) throws Exception {
		return attendanceDepartmentMapper.insert(attendanceDepartment);
	}

	public Integer update(AttendanceDepartment attendanceDepartment) throws Exception {
		return attendanceDepartmentMapper.updateByPrimaryKeySelective(attendanceDepartment);
	}

	public Integer updateOrder(AttendanceDepartmentUpdateOrder[] orders) throws Exception {
		int num = 0;
		for (AttendanceDepartmentUpdateOrder order: orders) {
			AttendanceDepartment attendanceDepartment = new AttendanceDepartment();
			attendanceDepartment.setId(order.getId());
			attendanceDepartment.setDisplayOrder(order.getDisplayOrder());
			num += attendanceDepartmentMapper.updateByPrimaryKeySelective(attendanceDepartment);
		}
		return num;
	}
	
	/*
	 * @category 逻辑删除
	 */
	public Integer delete(Integer id) throws Exception {
		int num = 0;
		AttendanceDepartment department = new AttendanceDepartment();
		department.setSuperDepartmentId(id);
		department.setIsValid(CommonConstant.INVALID);
		num += attendanceDepartmentMapper.updateBySuperDepartmentId(department);
		AttendanceDepartment attendanceDepartment = new AttendanceDepartment();
		attendanceDepartment.setId(id);
		attendanceDepartment.setIsValid(CommonConstant.INVALID);
		num += attendanceDepartmentMapper.updateByPrimaryKeySelective(attendanceDepartment);
		return num;
	}
	
}
